package com.leetcode;

/**
 * 303. 区域和检索 - 数组不可变
 * 前缀和, 另一种遍历方式
 *
 * @author fy
 * @date 2022-05-16 21:12
 */
public class Solution303_1 {

    class NumArray {
        private int[] sums;

        public NumArray(int[] nums) {
            int n = nums.length;
            // 数组初始化时默认值为0，所以sums[0]默认为0
            sums = new int[n + 1];
            for (int i = 0; i < n; i++) {
                sums[i + 1] = sums[i] + nums[i];
            }
        }

        public int sumRange(int i, int j) {
            return sums[j + 1] - sums[i];
        }
    }

}
